package com.jed.xxh.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.jed.xxh.R;
import com.jed.xxh.adapter.CourierAdapter;
import com.jed.xxh.entity.CourierData;
import com.jed.xxh.utils.L;
import com.jed.xxh.utils.StaticClass;
import com.jed.xxh.utils.T;
import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目名：SmartButler<br/>
 * 包名：  com.jed.xxh.ui<br/>
 * 作者：徐晓辉<br/>
 * 创建日期：2018/11/27 11:22<br/>
 * 描述：物流信息页面<br/>
 */
public class CourierActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private EditText et_name;
    private EditText et_number;
    private Button btn_get_courier;
    private ListView mListView;
    private List<CourierData> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_courier);
        initView();
    }

    private void initView() {
        et_name = findViewById(R.id.et_name);
        et_number = findViewById(R.id.et_number);
        L.i("要查询的物流公司名称：" + et_name.getText().toString());
        L.i("要查询的物流单号：" + et_number.getText().toString());
        btn_get_courier = findViewById(R.id.btn_get_courier);
        btn_get_courier.setOnClickListener(CourierActivity.this);
        mListView = findViewById(R.id.mListView);
        mListView.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_get_courier:
                getCourier();
                break;
        }
    }

    /**
     * 查询物流信息
     */
    private void getCourier() {
        String name = et_name.getText().toString().trim();
        String number = et_number.getText().toString().trim();
        boolean isEmpty = !TextUtils.isEmpty(name) && !TextUtils.isEmpty(number);
        if (isEmpty) {
            //post请求简洁版实现
            HttpParams params = new HttpParams();
            params.put("key", StaticClass.JH_COURIER_KEY);
            params.put("com", name);
            params.put("no", number);
            RxVolley.post(StaticClass.JH_COURIER_URL, params, new HttpCallback() {
                @Override
                public void onSuccess(String t) {
                    L.i("请求到的数据:" + t);
                    parsingJson(t);
                }
            });
        } else {
            T.showShortToast(this.getString(R.string.text_tost_empty));
        }
    }

    private void parsingJson(String t) {
        try {
            JSONObject jsonObject = new JSONObject(t);
            String error_code = jsonObject.getString("error_code");
            int code = Integer.parseInt(error_code);
            switch (code) {
                case 0:
                    JSONObject jsonResult = jsonObject.getJSONObject("result");
                    JSONArray jsonList = jsonResult.getJSONArray("list");
                    for (int i = jsonList.length() - 1; i >= 0; i--) {
                        JSONObject json = (JSONObject) jsonList.get(i);
                        CourierData courierData = new CourierData();
                        courierData.setDataTime(json.getString("datetime"));
                        courierData.setRemark(json.getString("remark"));
                        courierData.setZone(json.getString("zone"));
                        mList.add(courierData);
                    }
                    CourierAdapter courierAdapter = new CourierAdapter(mList);
                    mListView.setAdapter(courierAdapter);
                    break;
                case 204301:
                    T.showShortToast("不支持当前快递公司");
                    break;
                case 204302:
                    T.showShortToast("运单号错误");
                    break;
                case 204304:
                    T.showShortToast("查不到物流信息");
                    break;
                case 204303:
                default:
                    T.showShortToast("查询失败");
            }
        } catch (JSONException e) {
            T.showShortToast("查询失败");
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView viewById = view.findViewById(R.id.tv_remark);
        L.i("点击了：" + viewById.getText().toString());
        T.showShortToast("点击了：" + viewById.getText().toString());

    }
}
